package com.springboot.frame.comm.render;

import java.io.IOException;
import java.io.Writer;
import java.util.Map;

import com.springboot.frame.comm.exception.CodeGeneratorException;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.ClasspathResourceLoader;

/**
 * Beetl模板渲染
 *
 * @author madman
 */
@Data
@AllArgsConstructor
public class BeetlRender implements Render {

  private static final GroupTemplate ENGINE;

  static {
    try {
      ENGINE = new GroupTemplate(new ClasspathResourceLoader(),
          Configuration.defaultConfiguration());
    } catch (IOException e) {
      throw new CodeGeneratorException("BeetlRender 初始化失败", e);
    }
  }

  private String templatePath;

  @Override
  public void process(Map<String, Object> renderData, Writer writer) {
    Template template = ENGINE.getTemplate(templatePath);
    template.binding(renderData);
    template.renderTo(writer);
  }

}
